<?php

	class cmd_productCat extends mdl_productCat
	{
		//根据商品ID取得分类ID
		function getCatIdByGoodsId($goods_id)
		{
			$sql = 'SELECT `cat_id` from '.DB_PREFIX.'goods where goods_id=\''.$goods_id.'\'';
			$row = $this->db->selectrow($sql);
			return $row['cat_id'];
		}

		//根据分类ID取得分类信息
		function getCatById($cat_id)
		{
			//$aCat = $this->getTreeList($cat_id,'some');
			$sqlWhere = 'cat_id=\''.$cat_id.'\'';
			$aCat = $this->db->select('SELECT cat_name, cat_id, o.parent_id AS pid, o.p_order, o.cat_path, o.is_leaf AS cls,o.type_id, t.name AS type_name FROM sdb_goods_cat o
                    LEFT JOIN sdb_goods_type t ON o.type_id = t.type_id
                    WHERE '.$sqlWhere.' ORDER BY o.cat_path,o.p_order,o.cat_id');
			return $aCat;
		}

		//是否只显示子分类
		function onlyShowSubCat()
		{
			$uri = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:$_SERVER['QUERY_STRING'];
			if(strstr($uri,'product'))
			{
				preg_match('/.*product-(\d+)\.html/i',$uri,$match);
				$cat_id = $this->getCatIdByGoodsId($match[1]);
				return $this->getCatListById($cat_id);
			}
			elseif(strstr($uri,'gallery'))
			{
				preg_match('/.*gallery-(\d+)\.html/i',$uri,$match);
				$cat_id = $match[1];
				return $this->getCatListById($cat_id);
			}
			else return false;
		}

		//根据分类ID取得子分类
		function getCatListById($cat_id)
		{
			$aCat = $this->getTreeList($cat_id,'some');
			if (!$aCat)
			{
				$aCat = $this->getCatById($cat_id);
			}
			foreach ($aCat as $key=>$var)
			{
				$aCat[$key]['cat_path'] = ',';
			}
			return $aCat;
		}
	}
?>